package org.jetnuke.modules.news;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.jforum.dao.UserDAO;
import net.jforum.entities.User;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Autonews;
import org.jetnuke.entities.MainConfig;
import org.jetnuke.entities.Stories;

public class News implements IAction {

	int new_topic;

	public void setNew_topic(int new_topic) {
		this.new_topic = new_topic;
	}

	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {

		synchronized (Autonews.class) {
			
			Iterator it = DataAccessDriver.getInst().newAutoNewsDAO().selectDateGreater(new Date()).iterator();
			while (it.hasNext()) {
				Autonews news = (Autonews) it.next();
				Stories story = new Stories();
				story.setACOMM(news.getACOMM());
				story.setAID( news.getAID());
				story.setALANGUAGE(news.getALANGUAGE());
				story.setASSOCIATED(news.getASSOCIATED());
				story.setBODYTEXT(news.getBODYTEXT());
				story.setCATID(news.getCATID());
				story.setHOMETEXT(news.getHOMETEXT());
				story.setIHOME(news.getIHOME());
				story.setINFORMANT(news.getINFORMANT());
				story.setNOTES(news.getNOTES());
				story.setTITLE(news.getTITLE());
				story.setTOPIC(news.getTOPIC());
				DataAccessDriver.getInst().newStoriesDAO().addNew(story);
				
				DataAccessDriver.getInst().newAutoNewsDAO().deleteByID(news.getANID());
				
			}
		}

		
		MainConfig mainConfig = (MainConfig) request
		.getAttribute("MAIN_CONFIG");
		int storyhome=mainConfig.getTOP();
		
		int limit=storyhome;
		List stories=DataAccessDriver.getInst().newStoriesDAO().getStoriesByTopid_SidDesc(new_topic,limit);
		UserDAO um=DataAccessDriver.getInst().newUserDAO();
		Iterator iterator = stories.iterator();
		HashMap mp = new HashMap();
		HashMap topics = new HashMap();
		HashMap userMap=new HashMap();
		vc.put("mp", mp);
		vc.put("topics", topics);
		vc.put("userMap",userMap);
		while (iterator.hasNext()) {
			Stories story = (Stories) iterator.next();
			topics.put(new Integer(story.getSID()), DataAccessDriver.getInst().newTopicsDAO().retrieveByPK(story.getTOPIC()));
					
			float rated = 0;
			if (story.getSCORE() > 0) {
				rated=(float)(((double)story.getSCORE())/story.getRATINGS());
				
				//rated = story.getRATINGS() / story.getSCORE();
				mp.put(story, new Float(rated));
			} else {
				mp.put(story, new Float(0));
			}
			//User user=um.selectById(story.getAID());
			
			int uid=story.getAID();
			Integer itn=new Integer(uid);
			if(!userMap.containsKey(itn)){
				User user=um.selectById(uid);
				userMap.put(itn,user);
			}
			
			uid=story.getINFORMANT();
			itn=new Integer(uid);
			if(!userMap.containsKey(itn)){
				User user=um.selectById(uid);
				userMap.put(itn,user);
			}
			
		}
		vc.put("stories", stories);
		return new ActionForward(op, info);
	}

}
